var alarms = [];
var table;
function init() {
	$('#start').val($('#startTime').val());
	$('#end').val($('#endTime').val());
	$('#start').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		todayHighlight : true
	});
	$('#end').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		pickerPosition : 'bottom-left',
		todayHighlight : true
	});
	loadData();
}
function loadData() {
	var s = $('#start').val();
	var e = $('#end').val();
	$.getJSON("/rest/protectAlarm/getProtectDeviceAlarm/" + s + "/" + e + "/" + stationId, function(result) {
		alarms = result;
		alarms.sort(sortAlarm);
		$("title").html(alarms[0]['stationName'] + "  保护装置告警");
		loadHtml();
	});
}
function sortAlarm(obj1, obj2) {
	var t1 = obj1.eventStart ? obj1.eventStart : 0;
	t1 = t1.time ? t1.time : t1;
	var t2 = obj2.eventStart ? obj2.eventStart : 0;
	t2 = t2.time ? t2.time : t2;
	return t2 - t1;
}
function query() {
	loadData();
}
function loadHtml() {
	if (table) {
		table.getTable().destroy();
	}
	var columns = [ '告警时间', '保护名称', '告警内容', '主保护闭锁', '后备保护闭锁', '重合闸闭锁' ];
	var opt = MyTable.getDefaultOptions();
	opt.paging = true;
	opt.pageLength = 10;
	table = new MyTable("alarmTable", columns, opt);
	var datas = [];
	for ( var i in alarms) {
		var f = alarms[i];
		datas.push([ time(f.eventStart), str(f.protectName), str(f.alarmMsg), boo(f.firstlock), boo(f.secondlock),
				boo(f.thirdlock) ]);
		;
	}
	table.setDataSet(datas).show();
}
function str(v) {
	return v ? v : '';
}
function time(v) {
	if (!v) {
		return '';
	} else {
		return new Date(v.time ? v.time : v).toLocaleString();
	}
}
function boo(v) {
	return v ? '√' : '×';
}
function resizeHtml() {
	if (table) {
		table.getTable().draw();
	}
}
